package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.DepartmentJsonResult;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Log4j2
public class TestRestController {

    @Autowired
    private IDepartmentService service;

    @PostMapping("/departments")
    public JsonResult saveDepartment(Department department){
        service.save(department);
        return new JsonResult();
    }

    @DeleteMapping("/departments")
    public JsonResult delete(Long id){
        service.delete(id);
        return new JsonResult();
    }

    @GetMapping("/departments/{id}")
    public JsonResult selectDepartmentById(@PathVariable("id") Long id){

        log.trace("trace");
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        log.error("error");

        Department department = service.get(id);
        List<Department> list = new ArrayList<>();
        list.add(department);
        DepartmentJsonResult js = new DepartmentJsonResult();
        js.setList(list);
        return js;
    }

    @PatchMapping("/departments/{id}")
    public JsonResult update(@PathVariable("id") Long id,Department department){
        if (department.getSn().equals("")){
            department.setSn(null);
        }
        if (department.getName().equals("")){
            department.setName(null);
        }
        department.setId(id);
        service.update(department);
        return new JsonResult();
    }
}
